package pl.edu.agh.pros.proxy.view.xmpp;

import java.util.Arrays;

import com.vaadin.data.Item;
import com.vaadin.data.validator.IntegerValidator;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.TextField;

public class XmppServersForm extends Form {

	private static final long serialVersionUID = -6884510525756826439L;
	private static final String COMMON_FIELD_WIDTH = "15em";

	public XmppServersForm() {
		this.setCaption("General");
		this.setWriteThrough(false);
		this.setInvalidCommitted(false);
		this.setFormFieldFactory(new XmppFieldFactory());
		this.setVisibleItemProperties(Arrays.asList(new String[] { "hostname", "port", "conferenceService" }));
		this.setWriteThrough(false);
	}

	@SuppressWarnings("serial")
	private class XmppFieldFactory extends DefaultFieldFactory {
		@Override
		public Field createField(Item item, Object propertyId, Component uiContext) {

			if ("hostname".equals(propertyId)) {
				TextField tf = new TextField("Hostname");
				tf.setRequired(true);
				tf.setNullRepresentation("");
				tf.setWidth(COMMON_FIELD_WIDTH);
				return tf;
			} else if ("port".equals(propertyId)) {
				TextField tf = new TextField("Port");
				tf.setRequired(true);
				tf.setNullRepresentation("");
				tf.setWidth(COMMON_FIELD_WIDTH);
				tf.addValidator(new IntegerValidator("Please provide a proper port number"));
				return tf;
			} else if ("conferenceService".equals(propertyId)) {
				TextField tf = new TextField("Conference Service");
				tf.setRequired(true);
				tf.setNullRepresentation("");
				tf.setWidth(COMMON_FIELD_WIDTH);
				return tf;
			}
			return null;
		}
	}

}
